/*
 * Copyright (C) 2012 Le Tuan Anh <anh.takaji@gmail.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package ezcms.client;

import ezcms.dto.User;
import ezcms.ejb.UserAccountServiceRemote;
import ezcms.exceptions.BusinessLogicException;
import ezcms.exceptions.LookupException;
import ezcms.exceptions.PresentationException;
import ezcms.exceptions.ServiceInvokeException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Le Tuan Anh <anh.takaji@gmail.com>
 */
public class UserServiceDelegate {

    private static final Logger logger = Logger.getLogger(UserServiceDelegate.class.getName());
    private UserAccountServiceRemote remote = null;

    public UserServiceDelegate() throws LookupException {
        remote = lookupUserServiceFacade();
    }

    private UserAccountServiceRemote lookupUserServiceFacade() throws LookupException {
        EJBLocator locator = EJBLocator.getInstance();
        try {
            return locator.getUserServiceFacade();
        } catch (PresentationException ex) {
            //TODO: log
            Logger.getLogger(UserServiceDelegate.class.getName()).log(Level.SEVERE, null, ex);
            throw new LookupException(ex);
        }
    }

    /* Start service methods proxy */
    //
    public User getUser(String email) throws ServiceInvokeException {
        try {
            return remote.getUser(email);
        } catch (BusinessLogicException ex) {
            //TODO: log
            logger.log(Level.SEVERE, "Error while invoking service method getUser", ex);
            throw new ServiceInvokeException(ex);
        }
    }

    public void saveUser(User user) throws ServiceInvokeException {
        try {
            remote.saveUser(user);
        } catch (BusinessLogicException ex) {
            //TODO: log
            logger.log(Level.SEVERE, "Error while invoking service method saveUser", ex);
            throw new ServiceInvokeException(ex);
        }
    }

    public boolean authenticate(User user) throws ServiceInvokeException {
        try {
            return remote.authenticate(user);
        } catch (BusinessLogicException ex) {
            //TODO: log
            logger.log(Level.SEVERE, "Error while invoking service method authenticate", ex);
            throw new ServiceInvokeException(ex);
        }
    }

    public List<User> getAllUsers() throws ServiceInvokeException {
        try {
            return remote.getAllUsers();
        } catch (BusinessLogicException ex) {
            //TODO: log
            logger.log(Level.SEVERE, "Error while invoking service method authenticate", ex);
            throw new ServiceInvokeException(ex);
        }
    }
//    public void deleteUser(User user) throws ServiceInvokeException {
//        try {
//            remote.deleteUser(user);
//        } catch (BusinessLogicException ex) {
//            //TODO: log
//            logger.log(Level.SEVERE, "Error while invoking service method saveUser", ex);
//            throw new ServiceInvokeException(ex);
//        }
//    }
}
